package com.gxa.xc.rbac.Service.impl.impl;

import com.gxa.xc.rbac.Service.impl.MenuSerivce;
import com.gxa.xc.rbac.dto.ResultDTO;
import com.gxa.xc.rbac.mapper.PremissionMapper;
import com.gxa.xc.rbac.pojo.Admin;
import com.gxa.xc.rbac.pojo.Menu1;
import com.gxa.xc.rbac.pojo.Premission;
import com.gxa.xc.rbac.pojo.Role;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
@SuppressWarnings("all")
@MapperScan("com.gxa.xc.rbac.pojo.Menu")
public class MenuServiceImpl implements MenuSerivce {
    @Autowired
    private PremissionMapper premissionMapper;
    @Override
    public ResultDTO getMenu(HttpSession session) {
        List<Premission>premissionList=new ArrayList<>();
        Admin admin= (Admin) session.getAttribute("admin");
        System.out.println(admin.getId());
         if(isSupper(admin)){
             premissionList=premissionMapper.findAll();
         }
         else{
             for(Role role:admin.getRoles()){
                 premissionList.addAll(role.getPremissions());
                 System.out.println("role=="+role.getPremissions());
             }
         }
        List<Menu1> menus = new ArrayList<>();
        // 遍历 生成 一级菜单
        for (Premission premission : premissionList) {
            if(premission.getIsMune().equals(1) && premission.getPremissionLevel().equals(1)){
                // 构造出一级菜单
                Menu1 menu = new Menu1(premission.getId(), premission.getParentId(), premission.getPremissionName(), premission.getPremissionUrl());
                System.out.println("menu=="+menu);
                menus.add(menu);
            }
        }
        // 遍历添加二级菜单
        for (Menu1 menu : menus) {
            for (Premission premission : premissionList) {
                if(premission.getIsMune().equals(1) && premission.getPremissionLevel().equals(2) && premission.getParentId().equals(menu.getMenuId())){
                    // 当前的子级菜单
                    Menu1 childMenu = new Menu1(premission.getId(), premission.getParentId(), premission.getPremissionName(), premission.getPremissionUrl());
                    menu.getSubMenus().add(childMenu);
                }
            }

        }

        for(Menu1 menu1:menus){
            System.out.println("我的="+menu1);
        }
        return new ResultDTO(200,"success",menus);

    }

    @Override
    public boolean isSuper(HttpSession session) {
        Admin admin= (Admin) session.getAttribute("admin");
        if(admin==null)
            return false;
        return isSupper(admin);
    }

    @Override
    public boolean hasPremission(Admin admin, String url) {
        if(admin==null||url==null)
            return false;
          List<Premission>premissions=new ArrayList<>();
          for(Role role:admin.getRoles()){
              premissions.addAll(role.getPremissions());
          }
          for(Premission premission:premissions){
              if(StringUtils.isEmpty(premission.getPremissionUrl()))
                  return false;
              if(premission.getPremissionUrl().equals(url))
                  return true;
          }

          return false;
    }

    private boolean isSupper(Admin admin){
        for(Role role:admin.getRoles()){
            if(role.getId()==4){
                return true;
            }
        }
        return false;
    }
}
